(function() {

	var babyApp = angular.module('babyApp', [ '$http' ]);
	
	babyApp.controller('SimpleController', [ '$http', function($http) {

		var ctrl = this;
		ctrl.logs = [{
		    "timestamp" : 1399067760000,
		    "leftBreast" : 312,
		    "rightBreast" : 132,
		    "pumped" : 54,
		    "formula" : 55,
		    "diaper" : WE,
		    "updateUser" : "Amruta [ng]",
		    "updateTimestamp" : 1405979749000
		}];
		
		// --- http approach 1
		var myPromise = $http.get('http://localhost:7001/baby-rest-services/log/get');
		
		myPromise.success(function(data) {

			alert(angular.fromJson(data));
			ctrl.logs = data;
		});
		
		myPromise.error(function(data) {

			alert('ajax failed');
		});
		
		// --- http approach 2
		$http({
		    method : 'GET',
		    url : 'http://localhost:7001/baby-rest-services/log/get',
		}).success(function(data) {

			alert(angular.fromJson(data));
			ctrl.logs = data;
		}).error(function(data) {

			alert('ajax failed');
		});
		
		// --- http post approach 1
		$scope.saveLog = function() {

			var aNewLog = {
			    "timestamp" : 1399067760000,
			    "leftBreast" : $scope.newLog.leftBreast,
			    "rightBreast" : $scope.newLog.rightBreast,
			    "pumped" : $scope.newLog.pumped,
			    "formula" : $scope.newLog.formula,
			    "diaper" : $scope.newLog.diaper,
			    "updateUser" : "Amruta [ng]",
			    "updateTimestamp" : 1405979749000
			};
			
// $http.post('http://default-environment-2xgz38vab8.elasticbeanstalk.com/log/add/', aNewLog) //
			$http.post('http://localhost:7001/baby-rest-services/log/add', aNewLog) //
			.success(function(data, status, headers) {

				alert('.. in success ..');
				
				dataService.getData().then(function(dataResponse) {

					$scope.data = dataResponse.data;
				});
			});
		}

	} ]);
	
	babyApp.controller('LogController', [ '$scope', '$http', 'dataService', function($scope, $http, dataService) {

		$scope.data = [];
		
		dataService.getData().then(function(dataResponse) {

			alert(angular.fromJson(dataResponse));
			$scope.data = dataResponse.data;
		});
		
		$http({
		    method : 'GET',
		    url : 'http://localhost:7001/baby-rest-services/log/get',
		}).success(function(data) {

			alert(angular.fromJson(data));
			$scope.data = data.data;
		});
		
		$scope.saveLog = function() {

			var aNewLog = {
			    "timestamp" : 1399067760000,
			    "leftBreast" : $scope.newLog.leftBreast,
			    "rightBreast" : $scope.newLog.rightBreast,
			    "pumped" : $scope.newLog.pumped,
			    "formula" : $scope.newLog.formula,
			    "diaper" : $scope.newLog.diaper,
			    "updateUser" : "Amruta [ng]",
			    "updateTimestamp" : 1405979749000
			};
			
// $http.post('http://default-environment-2xgz38vab8.elasticbeanstalk.com/log/add/', aNewLog) //
			$http.post('http://localhost:7001/baby-rest-services/log/add', aNewLog) //
			.success(function(data, status, headers) {

				alert('.. in success ..');
				
				dataService.getData().then(function(dataResponse) {

					$scope.data = dataResponse.data;
				});
			});
		}

	} ]);
	
	babyApp.service('dataService', [ '$http', function($http) {

		delete $http.defaults.headers.common['X-Requested-With'];
		this.getData = function($http) {

			return $http({
			    method : 'GET',
			    url : 'http://localhost:7001/baby-rest-services/log/get',
			});
		}
	} ]);
})();